﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Newtonsoft.Json;
using AiShow.Model;
using AiShow.IBLL;
using AiShow.BLL;

namespace AiShow.WebSite
{

    /// <summary>
    /// 编辑个人资料页
    /// </summary>
    public partial class PersonalEdit : System.Web.UI.Page
    {
        private District district;
        //private int userId;

        private IUserManager userManager = new UserManager();
        private void BindProvince(string ProvinceName)
        {
            ddlProvince.DataSource = district.Provinces;
            ddlProvince.DataTextField = "ProvinceName";
            ddlProvince.DataValueField = "ProvinceName";
            ddlProvince.DataBind();
            ddlProvince.Items.Insert(0, "- 请选择 -");
            if (string.IsNullOrEmpty(ProvinceName))
            {
                ddlProvince.SelectedIndex = 0;
            }
            else
            {
                ddlProvince.SelectedValue = ProvinceName;
            }
            
        }
        private void BindCity(string ProvinceName,string CityName)
        {
            var Province = district.Provinces.SingleOrDefault(p =>p.ProvinceName == ProvinceName);
            if (Province != null)
            {
                ddlCity.DataSource = Province.Citys;
                ddlCity.DataTextField = "CityName";
                ddlCity.DataValueField = "CityName";
                ddlCity.DataBind();
            }
            ddlCity.Items.Insert(0, "- 请选择 -");
            if (string.IsNullOrWhiteSpace(CityName))
            {
                ddlCity.SelectedIndex = 0;
            }
            else
            {
                ddlCity.SelectedValue = CityName;
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            var file = Server.MapPath("~/App_Data/district.json");
            var text = File.ReadAllText(file, System.Text.Encoding.Default);
            district = JsonConvert.DeserializeObject<District>(text);

            if (!IsPostBack)
            {

                var ProvinceName = "";
                var CityName = "";
                var user = IdentityManager.ReadUser();
                if (user == null)
                {
                    Response.Redirect("login.aspx?ReturnUrl=PerSonalEdit.aspx");
                }
                else
                {
                    
                    //处理省市
                    if (!string.IsNullOrEmpty(user.Address)) { 
                    string[] addressArray = user.Address.Split(' ');
                    if (addressArray.Length == 2)
                    {
                        ProvinceName = addressArray[0];
                            CityName = addressArray[1];
                    }
                    }
                    //兴趣爱好
                    if (!string.IsNullOrWhiteSpace(user.Hobby))
                    {
                        string[] hobbys = user.Hobby.Split(',');
                        foreach(ListItem item in cblHobby.Items)
                        {
                            var hobby = item.Text;
                            item.Selected=hobbys.Any(p => p == hobby);

                        }
                    }
                    txtNickName.Text = user.NickName;
                    txtBirthday.Text = user.Birthday?.ToString("yyyy-MM-dd");
                    ddlSex.SelectedValue = user.Sex.ToString();
                }

                BindProvince(ProvinceName);
                BindCity(ProvinceName, CityName);
            }
        }

        protected void btnProfile_Click(object sender, EventArgs e)
        {
            var user = IdentityManager.ReadUser();
            user.NickName = txtNickName.Text;
            user.Address = ddlProvince.SelectedValue + " " + ddlCity.SelectedValue;
            user.Birthday = Convert.ToDateTime(txtBirthday.Text);
            user.Sex = Convert.ToInt32(ddlSex.SelectedValue);
            
            string hobby = "";
            foreach(ListItem item in cblHobby.Items)
            {
                if (item.Selected)
                {
                    hobby += item.Text + ",";
                }
            }
            if (hobby.Length > 1)
                user.Hobby = hobby.Substring(0, hobby.Length - 1);
            else
                user.Hobby = hobby;
            var result = userManager.EditWithoutAvatar(user);
            if (result.StatusCode == Common.StatusCode.Succeed)
            {
                Response.Redirect("Personal.aspx");
            }
            else
            {
                ClientScript.RegisterStartupScript(Page.GetType(), "loginerror", $"alert('{result.Message}')", true);
            }
            userManager.EditAvatar(user);
            
        }

        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            var p = ddlProvince.SelectedValue;
            BindCity(p, "");
        }
    }
}